const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');
const views = require('koa-views');
const path = require('path');
const static = require('koa-static');

const app = new Koa();
const router = new Router();
const levy = new Router();
const afaart = new Router();

levy
    .get('/', ctx => {
        ctx.body = 'levy index';
    })
    .get('/todo', ctx => {
        ctx.body = 'levy todo';
    })

afaart
    .get('/', ctx => {
        ctx.body = 'afaart index';
    })
    .get('/todo', ctx => {
        ctx.body = 'afaart todo';
    })

router
    .use('/levy',levy.routes(),levy.allowedMethods())
    .use('/afaart',afaart.routes(),afaart.allowedMethods())
    .get('/setCookie',async ctx => {
        ctx.cookies.set('userName','levy');
        ctx.body = 'cookie is setted already';
    })
    .get('/', async ctx => {
        title = 'new title';
        await ctx.render('index',{title});
    })
app
    .use(views(path.join(__dirname,'./views'),{
        extension : 'ejs'
    }))
    .use(static(path.join(__dirname,'./static')))
    .use(bodyParser())
    .use(router.routes())
    .use(router.allowedMethods())
    .listen(3000, () => {
        console.log('Server is running at port 3000 .');
    })